package org.jackysoft.struts2;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jackysoft.config.CachedQuery;
import org.jackysoft.config.ModuleContext;
import org.jackysoft.entity.BaseOption;
import org.jackysoft.entity.Identifiable;
import org.jackysoft.google.collection.ZTreeNode;
import org.jackysoft.query.LogicRelation;
import org.jackysoft.query.Query;
import org.jackysoft.query.QueryState;
import org.joda.time.DateTime;

@Results({
		@Result(name = "download", type = "stream", params = { "contentType",
				"${contentType}", "contentLength ", "${contentLength}",
				"contentDisposition ", "attachment;filename=${upLoadFileName}",
				"inputName ", "inputStream", "bufferSize", "1024" }),
		@Result(name = "stream", type = "stream", params = { "contentType",
				"application/octet-stream", "contentLength ",
				"${contentLength}", "contentDisposition ",
				"attachment;filename=${upLoadFileName}", "inputName ",
				"inputStream", "bufferSize", "1024" }),
		@Result(name = "fail", type = "dispatcher", params = { "location",
				"/jsp/commmon/fail.jsp" }

		),
		@Result(name = "jqgrid", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "pager" }

		),
		@Result(name = "beanlist", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "beans" }

		),
		@Result(name = "stringlist", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "stringList" }),
		@Result(name = "beandata", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "bean" }

		),
		@Result(name = "idlist", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "idList" }

		),
		@Result(name = "zbeanlist", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "zbeans" }),
		@Result(name = "mapdata", type = "json", params = {
				"excludeNullProperties", "true", "ignoreHierarchy", "false",
				"root", "resultMap" }

		)

})
@InterceptorRefs({ @InterceptorRef("defaultStack"),
		@InterceptorRef("crudInterceptor") })
public class UtilsCRUDAction<T extends Identifiable> extends
		SerializeAction<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4320636618467471006L;
	public static final String ZBEANS = "zbeanlist";
	public static final String DETAILS= "details";
	protected String search;
	protected String errorHtml;
	protected Query query;
	protected DateTime usedate;

	protected String term;
	protected Collection<String> stringList;   
	protected InputStream inputStream = null;
	protected File upLoad = null;
	protected long contentLength;
	protected String upLoadFileName = null;
	protected String upLoadContentType=null;
	protected String contentType = null;

	protected File[] upLoads = null;
	protected String[] upLoadsFileName = null;
	protected LogicRelation logicRelation;
	protected Collection<ZTreeNode> zbeans;
	protected Collection<BaseOption> options;
    
	protected CachedQuery cacheQuery;
	protected ModuleContext myContext;
	public UtilsCRUDAction() {
		super();
	}

	public String select() throws Exception {
		return SELECT;
	
	}

	public String download() throws Exception{
		return DOWNLOAD;
	}
	
	public String list() throws Exception {

		return LIST;
	}

	public String query() throws Exception {
		
		if(query==null)return QUERY;
		log.info("cache result code is  : "+cacheQuery.getResultCode());		
		if(!StringUtil.isBlank(cacheQuery.getResultCode())){
			log.info("cache result code is  : "+cacheQuery.getResultCode());
		    if(query.getState()==QueryState.PAGE
		    	&& null!=cacheQuery
		    	&& null!=cacheQuery.getResultCode()
		    		){ 
		    	query.setFields(cacheQuery.getFields());
		    	log.info("Action sqlId : "+cacheQuery.getResultCode());
		    	return cacheQuery.getResultCode();
		    
		    } else {
		    	log.info("query result is : "+query.getResultCode());
		    	return query.getResultCode();
		    }
		    
		 }
			
		return QUERY;
	}

	public String details() throws Exception{
		return DETAILS;
	}
	public String start() throws Exception {

		return SUCCESS;
	}
	
	public String newEdit() throws Exception{
		
		return NEWEDIT;
	}

	public String add() throws Exception {
		return SUCCESS;
	}

	public String remove() throws Exception {
		return REMOVE;
	}

	public String update() throws Exception {
		return UPDATE;
	}

	public String removes() throws Exception {
		return REMOVE;
	}

	public String updates() throws Exception {
		return UPDATE;
	}

	public String get() throws Exception {
		return SUCCESS;
	}

	public String creates() throws Exception {
		return CREATE;
	}

	public String create() throws Exception {
		return CREATE;
	}
	
	public String pager() throws Exception{
		return PAGER;
	}

	/**
	 * 上传动作,接应用户上传文件行为
	 * @author HANTU
	 * */
	public String upload() throws Exception {
		return SUCCESS;
	}

	public String uploads() throws Exception {
		return SUCCESS;
	}

	public String getone() throws Exception {

		return BEANDATA;
	}

	public String gets() throws Exception {
		return BEANLIST;
	}

	public String setting() throws Exception {
		return SUCCESS;
	}

	public String sorted() throws Exception {
		return SORTED;
	}
	
	public String ztree() throws Exception{
		return ZBEANS;
	}

	public String datalist() throws Exception {
		super.getJson();
		return JQGRID;
	}
	
	
	public String dataimport() throws Exception{
		return DATAIMPORT;
	}

	public String index() throws Exception {
		return INDEX;
	}

	public void setSearch(String search) {
		this.search = search;
		
	}

	public String getSearch() {
		return search;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public File getUpLoad() {
		return upLoad;
	}

	public void setUpLoad(File upLoad) {
		this.upLoad = upLoad;
	}

	public String getUpLoadFileName() {
		return upLoadFileName;
	}

	public void setUpLoadFileName(String upLoadFileName) {
		this.upLoadFileName = upLoadFileName;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public long getContentLength() {
		return contentLength;
	}

	public void setContentLength(long contentLength) {
		this.contentLength = contentLength;
	}

	public File[] getUpLoads() {
		return upLoads;
	}

	public void setUpLoads(File[] upLoads) {
		this.upLoads = upLoads;
	}

	public String[] getUpLoadsFileName() {
		return upLoadsFileName;
	}

	public void setUpLoadsFileName(String[] upLoadsFileName) {
		this.upLoadsFileName = upLoadsFileName;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public LogicRelation getLogicRelation() {
		return logicRelation;
	}

	public void setLogicRelation(LogicRelation logicRelation) {
		this.logicRelation = logicRelation;
	}

	public Collection<String> getStringList() {
		return stringList;
	}

	public void setStringList(Collection<String> stringList) {
		this.stringList = stringList;
	}

	public Query getQuery() {
		return query;
	}

	public void setQuery(Query query) {
		this.query = query;
	}

	public Collection<ZTreeNode> getZbeans() {
		return zbeans;
	}

	public void setZbeans(Collection<ZTreeNode> zbeans) {
		this.zbeans = zbeans;
	}

	public DateTime getUsedate() {
		return usedate;
	}

	public void setUsedate(DateTime usedate) {
		this.usedate = usedate;
	}

	public Collection<BaseOption> getOptions() {
		return options;
	}

	public void setOptions(Collection<BaseOption> options) {
		this.options = options;
	}

	public String getUpLoadContentType() {
		return upLoadContentType;
	}

	public void setUpLoadContentType(String upLoadContentType) {
		this.upLoadContentType = upLoadContentType;
	}
	
	@Resource
	public void setCacheQuery(CachedQuery cacheQuery) {
		this.cacheQuery = cacheQuery;
	}

	public  String getLastUrl() {
		return myContext.getLastUri();
	}

	public  void setLastUrl(String lastUrl) {
	    if(!StringUtil.isEmpty(lastUrl)){
	    	myContext.setLastUri(lastUrl);
	    }
		
	}

	public ModuleContext getMyContext() {
		return myContext;
	}

	@Resource
	public void setMyContext(ModuleContext myContext) {
		this.myContext = myContext;
	}

	public String getErrorHtml() {
		return errorHtml;
	}

	public void setErrorHtml(String errorHtml) {
		this.errorHtml = errorHtml;
	}

}
